package com.dyn.videohub.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dyn.videohub.Service.UserTokenService;
import com.dyn.videohub.domain.UserToken;
import com.dyn.videohub.mapper.UserTokenMapper;
import com.dyn.videohub.untils.DateUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service
public class UserTokenServiceImpl extends ServiceImpl<UserTokenMapper, UserToken> implements UserTokenService {
    public String createToken(Long userId) {
        UserToken userToken = new UserToken();
        userToken.setUserId(userId);
        String token = UUID.randomUUID().toString().replace("-","");
        userToken.setToken(token);
        Date createTime = new Date();
        userToken.setCreateTime(createTime);
        Date expireTime = DateUtils.addMinutes(createTime, 30);
        userToken.setExpireTime(expireTime);
        this.getBaseMapper().insert(userToken);
        return token;
    }

    @Override
    public UserToken getToken(String token) {
        LambdaQueryWrapper<UserToken> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        return this.baseMapper.selectOne(lambdaQueryWrapper.eq(UserToken::getToken,token));
    }
}
